#include "gtest/gtest.h"
#include "viml/compiler.h"
#include "viml/vm.h"

using namespace viml;

class FibonacciRecursiveTest : public ::testing::Test {
protected:
    Compiler compiler;
    VM vm;
    
    Value execute_source(const std::string& source) {
        auto bytecode = compiler.compile(source, "<test>");
        return vm.execute(bytecode);
    }
};

// 测试递归斐波那契函数 - 基础情况
TEST_F(FibonacciRecursiveTest, FibonacciRecursiveBaseCases) {
    std::string source = R"(
        function Fibonacci(n)
            if n <= 1
                return n
            endif
            
            return Fibonacci(n - 1) + Fibonacci(n - 2)
        endfunction
        
        let result0 = Fibonacci(0)
        let result1 = Fibonacci(1)
    )";
    
    Value result = execute_source(source);
    
    EXPECT_TRUE(vm.has_global("result0"));
    Value result0_val = vm.get_global("result0");
    EXPECT_TRUE(result0_val.is_number());
    EXPECT_EQ(result0_val.as_number(), 0.0);
    
    EXPECT_TRUE(vm.has_global("result1"));
    Value result1_val = vm.get_global("result1");
    EXPECT_TRUE(result1_val.is_number());
    EXPECT_EQ(result1_val.as_number(), 1.0);
}

// 测试递归斐波那契函数 - 小数值
TEST_F(FibonacciRecursiveTest, FibonacciRecursiveSmallValues) {
    std::string source = R"(
        function Fibonacci(n)
            if n <= 1
                return n
            endif
            
            return Fibonacci(n - 1) + Fibonacci(n - 2)
        endfunction
        
        let result5 = Fibonacci(5)
    )";
    
    Value result = execute_source(source);
    
    EXPECT_TRUE(vm.has_global("result5"));
    Value result5_val = vm.get_global("result5");
    EXPECT_TRUE(result5_val.is_number());
    EXPECT_EQ(result5_val.as_number(), 5.0);
}

// 测试递归斐波那契函数 - 稍大数值
TEST_F(FibonacciRecursiveTest, FibonacciRecursiveMediumValues) {
    std::string source = R"(
        function Fibonacci(n)
            if n <= 1
                return n
            endif
            
            return Fibonacci(n - 1) + Fibonacci(n - 2)
        endfunction
        
        let result8 = Fibonacci(8)
    )";
    
    Value result = execute_source(source);
    
    EXPECT_TRUE(vm.has_global("result8"));
    Value result8_val = vm.get_global("result8");
    EXPECT_TRUE(result8_val.is_number());
    EXPECT_EQ(result8_val.as_number(), 21.0);
}

// 测试递归斐波那契函数 - 较大数值
TEST_F(FibonacciRecursiveTest, FibonacciRecursiveLargerValues) {
    std::string source = R"(
        function Fibonacci(n)
            if n <= 1
                return n
            endif
            
            return Fibonacci(n - 1) + Fibonacci(n - 2)
        endfunction
        
        let result10 = Fibonacci(10)
    )";
    
    Value result = execute_source(source);
    
    EXPECT_TRUE(vm.has_global("result10"));
    Value result10_val = vm.get_global("result10");
    EXPECT_TRUE(result10_val.is_number());
    EXPECT_EQ(result10_val.as_number(), 55.0);
}